package course.ya.doc.from.code.model.statements;

/**
 *
 * @author smiling_devilll
 */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;

@Entity
@DiscriminatorValue("ForStatement")
public class ForStatement extends AStatement {

    @Column(name = "for_statement_condition")
    private String condition;
    @Transient
    private Pattern pattern;
    @Transient
    private Matcher matcher;
    @Transient
    private static final String STRING_PATTERN = "^for\\s*[(]  (.*;.*;.* | \\s*[A-z]\\w*\\s[A-z]\\w*\\s*[:].+) [)]$";

    public ForStatement() {
    }

    public ForStatement(String str) {
        this.pattern = Pattern.compile(STRING_PATTERN, Pattern.COMMENTS);
        this.condition = findStr(str);
    }

    public boolean validate(final String str) {
        matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public String findStr(String input) {
        matcher = pattern.matcher(input);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            return null;
        }
    }

    @Override
    public String toString() {
        return condition;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 13 * hash + (this.condition != null ? this.condition.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ForStatement other = (ForStatement) obj;
        if ((this.condition == null) ? (other.condition != null) : !this.condition.equals(other.condition)) {
            return false;
        }
        return true;
    }

}
